package newXgame.utils.list
{
	import anshe.object.ExTextField;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import imvu.magicpainter.images.imglist.sub.SubMenuIteam;
	
	import newXgame.utils.list.event.*;
	import newXgame.utils.list.struct.SuperStruct;
	
	public class ImageListObject extends Sprite
	{		
		private var m_base:String	= "http://system.anshechung.com/0807/";
		private var m_path:String	= "f_get_imagelist.php?uid=";
		private var uID:String 		= "1435605";
		
		private var menuList:MainListStruct;
		private var subArray:Array;
		private var selectData:SuperStruct;
		
		public function ImageListObject(userID:String = "", bPath:String = "", mPath:String = "")
		{
			m_base = (bPath.length > 0) ? bPath : m_base;
			m_path = (mPath.length > 0) ? mPath : m_path;
			uID = (userID.length > 0) ? userID : uID;
			
			subArray = new Array();
			
			menuList = new MainListStruct();
			menuList.addEventListener(Event.COMPLETE, onLoadDone);
		}
		
		//	初始化颜色
		public function InitColors(bColor:Number = -1, lColor:Number = -1):void
		{
			ListGlobal.BaseColor = (bColor < 0) ? ListGlobal.BaseColor : uint(bColor);
			ListGlobal.LightColor = (lColor < 0) ? ListGlobal.LightColor : uint(lColor);
		}
		
		//	开始读取档案
		public function StartLoad(userID:String = "", bPath:String = "", mPath:String = ""):void
		{			
			m_base = (bPath.length > 0) ? bPath : m_base;
			m_path = (mPath.length > 0) ? mPath : m_path;
			uID = (userID.length > 0) ? userID : uID;

			menuList.StartLoad(uID, m_base, m_path);			
		}
		
		//	取得当前的数据项
		public function get data():SuperStruct
		{
			return selectData;
		}
		
		//	删除对象
		public function Hidden():void
		{	
			//	清除旧的对象事件
			for(var i:int = 0; i < this.subArray.length; i++)
			{
				var subIteam:SubMenuIteam = this.subArray[i];				
				subIteam.removeEventListener(IMGEvent.CLICK_THIS, onClickIteam);
				subIteam.removeEventListener(IMGEvent.SHOW_MENU, onShowMenu);
				subIteam.removeEventListener(IMGEvent.HIDDEN_MENU, onHiddenMenu);
				
				subIteam.Hidden();
			}
			menuList =  null;	
		}
	
		//	树型结构读取完毕
		private function onLoadDone(e:Event):void
		{
			menuList.removeEventListener(Event.COMPLETE, onLoadDone);
			
			// trace(menuList.data);
		
			//**
			for(var i:int = 0; i < menuList.data.length; i++)
			{
				var subIteam:SubMenuIteam = new SubMenuIteam(menuList.data[i]);
				if(subArray.length > 0)
				{
					subIteam.y = subArray[subArray.length - 1].y + subArray[subArray.length - 1].objectHeight;
				}
				
				subIteam.addEventListener(IMGEvent.CLICK_THIS, onClickIteam);
				subIteam.addEventListener(IMGEvent.SHOW_MENU, onShowMenu);
				subIteam.addEventListener(IMGEvent.HIDDEN_MENU, onHiddenMenu);
				this.addChild(subIteam);
					
				subArray.push(subIteam);				
			}
			
			this.dispatchEvent(e);
			//*/
		}
				
		//	点击相关对象
		private function onClickIteam(e:Event):void
		{
			var sub:SubMenuIteam = e.target as SubMenuIteam;
			
			// trace("click done.." + Boolean(sub.clickData is MenuDataStruct));
			
			if(sub.clickData is MenuDataStruct)
			{
				//	如果选择的是图片
				selectData = sub.clickData;
				// trace("now Select img is " + sub.clickData);
				
				this.dispatchEvent(e);
			}
			sub.RushMenuPlace(sub);
		}
		
		//	隐藏事件
		private function onHiddenMenu(e:Event):void
		{
			(e.target as SubMenuIteam).RushMenuPlace(e.target as SubMenuIteam);
		}
		
		//	显示事件
		private function onShowMenu(e:Event):void
		{
			(e.target as SubMenuIteam).RushMenuPlace(e.target as SubMenuIteam);
		}
	}
}